﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class KPIReader
    {
        private const string SELECT_STMT =
            "SELECT " +
                 "KPIID " +
               ", Name " +
               ", Description " +
               ", Value " +
               ", Threshold1 " +
               ", Threshold2 " +
            "FROM KPI ";

        public KPI ReadByID(long id)
        {
            var statement = SELECT_STMT + "WHERE KPIID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", id));

            return DbTool<KPI>.ReadSingle(statement, parameters, CreateKPI);
        }
        public List<KPI> ReadAll()
        {
            var statement = SELECT_STMT + ";";

            return DbTool<KPI>.ReadMultiple(statement, null, CreateKPI);
        }
        private KPI CreateKPI(SQLiteDataReader rd)
        {
            KPI result = new KPI();
            result.ID = (long)rd["KPIID"];
            result.Name = (string)rd["Name"];
            if (rd.IsDBNull(rd.GetOrdinal("Description")))
                result.Description = "";
            else
                result.Description = (string)rd["Description"];
            result.Value = (decimal)rd["Value"];
            result.Threshold1 = (decimal)rd["Threshold1"];
            result.Threshold2 = (decimal)rd["Threshold2"];

            return result;
        }
    }
}
